package com.example.demo.controller;

import com.dhcc.csmsearch.elasticsearch.common.ElasticsearchManage;
import com.example.demo.core.entity.BulkResponseBody;
import com.example.demo.core.entity.RestResult;
import com.example.demo.core.enums.ElasticTypeEnum;
import com.example.demo.core.utils.ResultUtil;
import com.example.demo.mock.MockData;
import com.example.demo.mock.MockUtil;
import com.example.demo.service.ElasticBulkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutionException;

import static com.example.demo.core.enums.ElasticTypeEnum.*;
import static com.example.demo.core.enums.ElasticTypeEnum.DIAGNOSE;

@RestController
@RequestMapping("/mock")
public class MockController {

    @Resource(name = "env")
    private String curEnv;

    @Resource
    private ElasticsearchManage elasticsearchManage;

    @Resource
    private ElasticBulkService elasticBulkService;

    @GetMapping("/{index}/{theme}")
    public RestResult mockDianose(
            @PathVariable String index, @PathVariable String theme,
            @RequestParam(value = "total", defaultValue = "1000") int total) throws InterruptedException, ExecutionException {
        MockUtil mock = new MockUtil(curEnv, total, elasticsearchManage);

        List<MockData> mockDataList =  mock.getRandomForES();
        List<Map<String, Object>> mocks =  null;
        ElasticTypeEnum typeEnum = getByTheme(theme);
        if(typeEnum == null){
            return ResultUtil.error("找不到" + theme + "对应的ES类型");
        }
        switch (typeEnum){
            case DIAGNOSE:
                mocks = mock.buildDiagnoseData(mockDataList);
                break;
            case ORDITEM:
                mocks = mock.buildOrdItemData(mockDataList);
                break;
            case OrdRis:
                mocks = mock.buildOrdRisData(mockDataList);
                break;
            case Lisitem:
                mocks = mock.buildLisitemData(mockDataList);
                break;
            default: break;
        }
        BulkResponseBody responseBody = elasticBulkService.bulk(theme, mocks);

        return ResultUtil.success(responseBody);
    }
}
